<?php Yii::app()->clientScript->registerCssFile('css/InvoiceReport.css');?>
<?php Yii::app()->clientScript->registerCssFile('css/ContractReport.css');
$this->layout = '//layout/space';
Yii::app()->clientScript->registerCoreScript('jquery');
?>
<script>
    $(document).ready(function(){
       
    });
    function toggleCanceledContract(){
        $('.contractStatus[value="4"]').parent().parent().toggle();
    }
</script>
<div id="invoice-report">
    <div id="title">
        QSI, INC<br/>
        <?php echo date('Y-m-d'); ?><br/>
        Service Contract Report <br/>
        Company : <?php echo $customerModel->firstname.' '.$customerModel->lastname; ?>
    </div>
    
    <div id="content">
        <?php 
        $table=new CI_Table();
        $table->set_heading(array(
            'Contract ID',
            'Address',
            'City, State',
            'Start Date',
            'End Date',
            'Total Cost',
            'Billing',
            '<a href="javascript:void(0)" onclick="toggleCanceledContract()">Status</a>'
        ));
        $table->set_template(array(
            'table_open'=>'<table class="zeebra">'
        ));
        foreach($customerModel->serviceContractModelList as $contract){
            /* @var $contract Contracts */            
            $billingHtml='';
            if($contract->billtype==Contracts::TYPE_YEARLY){
                $billingHtml=$contract->contractamount;
            }
            else{
                foreach($contract->schedulePaymentModelList as $payment){
                    $paymentDate=DateTime::createFromFormat('Y-m-d', $payment->date);
                    $curDate=new DateTime('now');
                    $interval=$curDate->diff($paymentDate, false);
                  
                    if(($interval->format('%m')>0 || $interval->format('%y')>0 || $interval->format('%d')>30) 
                        && strtotime('now') > strtotime($payment->date)){
                        $billingHtml.='<div style="color:red;">';
                    }elseif(($interval->format('%m')==0 && $interval->format('%y')==0 && $interval->format('%d')<30 && $interval->format('%d')>0)) {
                        $billingHtml.='<div style="color:green;">';
                    }else{
                        $billingHtml.='<div>';
                    }
                    $billingHtml .= $paymentDate->format('m-d-Y').' - '.$payment->amount;
                    if($payment->invoice!=''){
                        $billingHtml.=' - '.$payment->invoice;
                    }
                    $billingHtml.='</div><br/>';
                }
            }
            $address=$contract->location!=null?$contract->location->add1:'';
            $city=$contract->location!=null?$contract->location->city.', '.$contract->location->state:'';
            $contractStatusField = "<input type='hidden' class='contractStatus' value='$contract->status'/>";
            
            $table->add_row(array(
                $contract->contractid,
                $address,
                $city,
                $contract->contractstartdate,
                $contract->contractenddate,
                number_format($contract->contractamount,2),
                $billingHtml,
                ContractStatus::getText($contract->status).$contractStatusField
            ));
        }
        echo $table->generate();
        
        $table->clear();
        echo '<br/>';
        echo '<b>Total</b><br/>';
        
        $table->set_template(array(
            'table_open'=>'<table class="zeebra">'
        ));
        $table->set_heading(array(
            'Number of contracts','Total cost','Payments'
        ));
        $totalScheduleListHtml='';
        if($billtype!=Contracts::TYPE_YEARLY){
            foreach($schedulePaymentList as $number=>$period){
                $subTotal=0;
                foreach($period as $amount){
                    $subTotal+=$amount;
                }
                $totalScheduleListHtml.= 'Payment #'.$number.': '.number_format($subTotal,2).'<br/>';
            }
        }else{
            $totalScheduleListHtml = number_format($totalAmount,2);
        }
        $table->add_row(
                count($customerModel->serviceContractModelList),
                number_format($totalAmount,2),
                $totalScheduleListHtml
                );
        echo $table->generate();
        ?>
        <fieldset>
            <legend>Description</legend>
            Green - Current<br/>
            Red   - Past Due
        </fieldset>
    </div>
</div>